package com.reflect.demo02;

public class Test {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        System.out.println("获取反射Class实例的方式");
        // 方式一：通过 forName 获取 填写完整的包名
        // 但需要抛出 ClassNotFoundException 异常
        Class cl = Class.forName("com.reflect.demo02.Work");
        // 方式二：通过 对象.getClass() 获取
        Work work = new Work();
        Class cl2 =  work.getClass();
        // 方式三：通过 类名.class 获取
        Class cl3 = Work.class;
        /**
         * Class 类的常用方法
         * ClassForName
         */
        cl3.newInstance();
        Person person = new Student();
        Class cls = person.getClass();
        // 获取父类
        Class superclass = cls.getSuperclass();
        System.out.println(superclass); // class com.reflect.demo02.Person

        System.out.println("==========");
        System.out.println(cls.hashCode()); // 1608446010
        Student student = new Student();
        Class cls2 = student.getClass();
        System.out.println(cls2.hashCode()); // 1608446010
        Person person1 = new Person();
        Class cls3 = person1.getClass();
        System.out.println(cls3.hashCode()); // 1451270520

    }
}

class Person {
    public String name;

    public Person() {
    }
    public Person(String name) {
        this.name = name;
    }
}

class Student extends Person {
    public Student() {
        this.name = "学生";
    }
}

class Teacher extends Person {
    public Teacher() {
        this.name = "学生";
    }
}
